const houseTypeData = require("./houseTypeData.js");

Component({
    properties: {
    },
    data: {
        ...houseTypeData,
        contentShow: null,
        priceShow: 0,
        isMaskShow: true,
        customMinTotal: null,
        customMaxTotal: null,
        customMinAvg: null,
        customMaxAvg: null,
        customMinTotalValue: "",
        customMaxTotalValue: "",
        customMinAvgValue: "",
        customMaxAvgValue: ""
    },
    
    methods: {
        // 重置select遍历方法
        resetSelect(data){
            data.forEach(ele => {
                ele.select = false;
            });
            return data;
        },
        // 组件数据不会自动刷新，此方法重置初始数据
        initData(){
            const that = this;
            let {areaData, 
                priceTitle,
                priceTotal, 
                priceSingle, 
                houseData, 
                saleStatus, 
                saleTime, 
                sortData
                } = this.data;

                this.setData({
                    areaData: that.resetSelect(areaData), 
                    priceTitle: that.resetSelect(priceTitle),
                    priceTotal: that.resetSelect(priceTotal), 
                    priceSingle: that.resetSelect(priceSingle), 
                    houseData: that.resetSelect(houseData), 
                    saleStatus: that.resetSelect(saleStatus), 
                    saleTime: that.resetSelect(saleTime), 
                    sortData: that.resetSelect(sortData),
                })
                this.creatParam();
        },
        // 标题切换交互绑定
        headerSelect(e){
            const index = e.target.dataset.index;
            let headerTitle = this.data.headerTitle;
            let select = headerTitle[index].select;
            if (select) {
                headerTitle.forEach(ele => {
                    ele.select = false;
                });
                this.setData({contentShow: null, isMaskShow: true, headerTitle})
            } else {
                headerTitle.forEach(ele => {
                    ele.select = false;
                });
                headerTitle[index].select = true;
                this.setData({
                    contentShow: index,
                    headerTitle,
                    isMaskShow: false,
                });
            }
        },
        // 面积选择交互绑定
        areaSelect(e){
            const index = e.target.dataset.index;
            let areaData = this.data.areaData;
            let select = areaData[index].select;
            areaData[index].select = select? false : true;
            this.setData({
                areaData,
            });
        },
        // 面积选项卡清空选项
        areaClear(){
            const that = this;
            let areaData = this.data.areaData;
            this.setData({
                areaData: that.resetSelect(areaData)
            })
        },
        // 更多选项卡清空选项
        moreClear(){
            const that = this;
            let {saleStatus, saleTime} = this.data;
            this.setData({
                saleStatus: that.resetSelect(saleStatus),
                saleTime: that.resetSelect(saleTime),
            })
        },
        // 房型选项卡清空选项
        houseClear(){
            const that = this;
            let houseData = this.data.houseData;
            this.setData({
                houseData: that.resetSelect(houseData)
            })
        },
        // 单价选项卡清空选项
        priceSingleClear(){
            const that = this;
            let priceSingle = this.data.priceSingle;
            this.setData({
                priceSingle: that.resetSelect(priceSingle)
            })
        },
        // 总价选项卡清空选项
        priceTotalClear(){
            const that = this;
            let priceTotal = this.data.priceTotal;
            this.setData({
                priceTotal: that.resetSelect(priceTotal)
            })
        },
        // 单价和总价切换交互绑定
        priceTitleShow(e){
            const index = e.target.dataset.index;
            const priceTitle = this.resetSelect(this.data.priceTitle);
            priceTitle[index].select = true;
            this.setData({priceShow: index, priceTitle,})
            
        },
        // UI交互数据绑定方法: 总价点击绑定
        priceTotalTap(e){
            const index = e.target.dataset.index;
            console.log('index', index)
            let priceTotal = this.resetSelect(this.data.priceTotal);
            let priceSingle = this.resetSelect(this.data.priceSingle);
            if (index != 0) {
              priceTotal[index].select = true;
            }
            this.setData({
                priceTotal,
                priceSingle,
                customMinTotalValue: "",
                customMaxTotalValue: "",
                customMinAvgValue: "",
                customMaxAvgValue: "",
                customMinTotal: "",
                customMaxTotal: "",
                customMinAvg: "",
                customMaxAvg: "",
            })
            this.customTotalSure();
        },
        // UI交互数据绑定方法: 单价点击绑定
        priceSingleTap(e){
            const index = e.target.dataset.index;
            console.log('index', index)
            let priceSingle = this.resetSelect(this.data.priceSingle);
            let priceTotal = this.resetSelect(this.data.priceTotal);
            if (index != 0) {
            priceSingle[index].select = true;
            }
            this.setData({
                priceSingle,
                priceTotal,
                customMinTotalValue: "",
                customMaxTotalValue: "",
                customMinAvgValue: "",
                customMaxAvgValue: "",
                customMinTotal: "",
                customMaxTotal: "",
                customMinAvg: "",
                customMaxAvg: "",
            })
            this.customAvgSure();
        },
        houseItemTap(e){
          const index = e.currentTarget.dataset.index;
            let houseData = this.data.houseData;
            let select = houseData[index].select;
            houseData[index].select = select? false : true;
            this.setData({houseData})
        },
        saleStatusTap(e){
            const index = e.target.dataset.index;
            let saleStatus = this.data.saleStatus;
            let select = saleStatus[index].select;
            saleStatus[index].select = select? false : true;
            this.setData({
                saleStatus,
            })
        },
        saleTimeTap(e){
            const index = e.target.dataset.index;
            let saleTime = this.data.saleTime;
            let select = saleTime[index].select;
            saleTime[index].select = select? false : true;
            this.setData({
                saleTime,
            })
        },
        // 排序方法触发
        sortItemTap(e){
            // const {customMinTotal, customMaxTotal, customMinAvg, customMaxAvg} = this.data;
            const index = e.target.dataset.index;
            let sortData = this.resetSelect(this.data.sortData);
            sortData[index].select = true;
            this.setData({sortData});
            let param = this.creatParam();
            this.subParam(param);
        },
        // 遮罩层隐藏方法
        hideMask(){
            this.setData({
                isMaskShow: true,
                contentShow: null,
                headerTitle: this.resetSelect(this.data.headerTitle),
            })
            // this.creatParam();
        },
        // 用户自定义input数据绑定
        customMinTotalBind(e){
            this.setData({customMinTotal: e.detail.value});
        },
        customMaxTotalBind(e){
            this.setData({customMaxTotal: e.detail.value});
        },
        customMinAvgBind(e){
            this.setData({customMinAvg: e.detail.value});
        },
        customMaxAvgBind(e){
            this.setData({customMaxAvg: e.detail.value});
        },
        // 用户自定义价格 button 点击事件
        customTotalSure(){
            let param = this.creatParam();
            const {customMinTotal, customMaxTotal, headerTitle} = this.data;
            if (customMinTotal && customMaxTotal) {
                let priceTotal = this.resetSelect(this.data.priceTotal);
                let priceSingle = this.resetSelect(this.data.priceSingle);
                // headerTitle[1].num = 0;
                this.setData({
                    headerTitle,
                    priceTotal, 
                    priceSingle,
                    customMinAvg: "",
                    customMaxAvg: "",
                });
            }
            this.subParam(param)
        },
        customAvgSure(){
            let param = this.creatParam();
            const {customMinAvg, customMaxAvg, headerTitle} = this.data;
            if (customMinAvg && customMaxAvg) {
                let priceTotal = this.resetSelect(this.data.priceTotal);
                let priceSingle = this.resetSelect(this.data.priceSingle);
                // headerTitle[1].num = 0;
                this.setData({
                    headerTitle,
                    priceSingle, 
                    priceTotal,
                    customMinTotal: "",
                    customMaxTotal: "",
                });
            }
            
            this.subParam(param)
        },
        subParam(param){
            if (JSON.stringify(param) === "{}" || param === "" || param === null || param === undefined) {
                param = this.creatParam();
            }
            this.triggerEvent('myevent', param);
            this.hideMask();
            console.log('param', param);
        },
        subBtn(){
            this.subParam();
        },
        // 创建组装参数方法
        creatParam(){
            let {areaData, 
                priceTotal, 
                priceSingle, 
                houseData, 
                saleStatus, 
                saleTime, 
                sortData, 
                headerTitle,
                customMinTotal,
                customMaxTotal,
                customMinAvg,
                customMaxAvg,
                } = this.data;
            let area = [], // 面积列表
            minTotal = "", // 户型最低总价
            maxTotal = "", // 户型最高总价
            minAvg = "", // 楼盘最低单价
            maxAvg = "", // 楼盘最高单价
            roomCount = [], // 房型
            saleStatusKey = [], // 销售状态列表
            openTime = [], // 开盘时间列表
            sortFlag = "", // 排序标识
            sort = "",
            areaNum = 0,
            priceNum = 0,
            houseNum = 0,
            moreNum = 0,
            sortNum = 0; 
            // 排序方式

            areaData.forEach(ele => {
                if (ele.select === true) {
                    area.push(ele.key)
                    areaNum ++;
                }
            })

            if (customMinTotal && customMaxTotal) {
                minTotal = customMinTotal;
                maxTotal = customMaxTotal;
                priceNum = 1;
            } else {
                priceTotal.forEach(ele => {
                    if (ele.select === true) {
                        minTotal = ele.key.minTotal;
                        maxTotal = ele.key.maxTotal;
                        priceNum = 1;
                    }
                })
            }

            if (customMinAvg && customMaxAvg) {
                minAvg = customMinAvg;
                maxAvg = customMaxAvg;
                priceNum = 1;
            } else {
                priceSingle.forEach(ele => {
                    if (ele.select === true) {
                        minAvg = ele.key.minAvg;
                        maxAvg = ele.key.maxAvg;
                        priceNum = 1;
                    }
                })
            }

            houseData.forEach(ele => {
                if (ele.select === true) {
                    roomCount.push(ele.key);
                    houseNum ++;
                }
            })

            saleStatus.forEach(ele => {
                if (ele.select === true) {
                    saleStatusKey.push(ele.key);
                    moreNum ++
                }
            })

            saleTime.forEach(ele => {
                if (ele.select === true) {
                    openTime.push(ele.key);
                    moreNum ++;
                }
            })

            sortData.forEach(ele => {
                if (ele.select === true) {
                    sortFlag = ele.key.sortFlag;
                    sort = ele.key.sort;
                    sortNum = 1;
                }
            })
            //debugger
            headerTitle[0].num = areaNum;
            headerTitle[1].num = priceNum;
            headerTitle[2].num = houseNum;
            headerTitle[3].num = moreNum;
            headerTitle[4].num = sortNum;
            this.setData({
                headerTitle
            })

            return {
                area,
                minTotal,
                maxTotal,
                minAvg,
                maxAvg,
                roomCount,
                saleStatus: saleStatusKey,
                openTime,
                sortFlag,
                sort
            };
        }
    }
  })